<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.26.5/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <script crossorigin="anonymous" src="/__dist__/botframework-webchat-fluent-theme.production.min.js"></script>
    <style>
      .flair {
        border-radius: inherit;
        outline: solid 2px red;
        outline-offset: -2px;
      }

      .flair--ongoing {
        outline-color: green;
        border-radius: var(--webchat-borderRadiusXLarge);
      }

      .loader {
        border-bottom: solid 4px blue;
      }
    </style>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="text/babel">
      const streamingFormat = new URL(window.location.href).searchParams.get('streamingFormat') ?? 'channelData'; // can be entity or channelData

      function addLivestreamingMetadata(activity, livestreamingMetadata) {
        return streamingFormat === 'entity'
          ? {
              ...activity,
              entities: [...(activity.entities ?? []), { ...livestreamingMetadata, type: 'streaminfo' }]
            }
          : {
              ...activity,
              channelData: { ...activity.channelData, ...livestreamingMetadata }
            };
      }

      run(async function () {
        const {
          React,
          ReactDOM: { render },
          WebChat: {
            decorator: { createActivityBorderMiddleware, DecoratorComposer },
            FluentThemeProvider,
            ReactWebChat
          }
        } = window; // Imports in UMD fashion.

        function Flair({ request, Next, ...props }) {
          return (
            <div className={request.livestreamingState === 'completing' ? 'flair' : ''}>
              <Next {...props} />
            </div>
          );
        }

        function FlairOngoing({ request, Next, ...props }) {
          return (
            <div className={request.livestreamingState === 'ongoing' ? 'flair flair--ongoing' : ''}>
              <Next {...props} />
            </div>
          );
        }

        function Loader({ request, Next, ...props }) {
          return (
            <div className={request.livestreamingState === 'preparing' ? 'loader' : ''}>
              <Next {...props} showLoader={false} />
            </div>
          );
        }

        const decoratorMiddleware = [
          createActivityBorderMiddleware(Flair),
          createActivityBorderMiddleware(FlairOngoing),
          createActivityBorderMiddleware(Loader)
        ];

        const { directLine, store } = testHelpers.createDirectLineEmulator();

        const activityStatusMiddleware = [() => () => () => false];

        const App = () => (
          <ReactWebChat activityStatusMiddleware={activityStatusMiddleware} directLine={directLine} store={store} />
        );

        render(
          <FluentThemeProvider>
            <DecoratorComposer middleware={decoratorMiddleware}>
              <App />
            </DecoratorComposer>
          </FluentThemeProvider>,
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        // WHEN: Informative message arrives.
        await directLine.emulateIncomingActivity(
          addLivestreamingMetadata(
            {
              from: {
                id: 'u-00001',
                name: 'Bot',
                role: 'bot'
              },
              id: 't-00001',
              text: 'Searching your document library…',
              type: 'typing'
            },
            { streamSequence: 1, streamType: 'informative' }
          )
        );

        // THEN: Informative message should appear.
        await pageConditions.numActivitiesShown(1);
        await pageConditions.became(
          'informative message show up',
          () => pageElements.activityContents()[0].textContent === 'Searching your document library…',
          1_000
        );

        // THEN: Should show the informative message with blue bottom border.
        // THEN: Should not show typing indicator.
        await host.snapshot('local');

        // WHEN: Interim activity arrives.
        await directLine.emulateIncomingActivity(
          addLivestreamingMetadata(
            {
              from: {
                id: 'u-00001',
                name: 'Bot',
                role: 'bot'
              },
              id: 't-00002',
              text: 'A quick brown fox',
              type: 'typing'
            },
            { streamId: 't-00001', streamSequence: 2, streamType: 'streaming' }
          )
        );

        // THEN: Interim activity should appear.
        await pageConditions.numActivitiesShown(1);
        await pageConditions.became(
          'interim activity show up',
          () => pageElements.activityContents()[0].textContent === 'A quick brown fox',
          1_000
        );

        // THEN: Should show the informative message with blue bottom border.
        // THEN: Should not show typing indicator.
        await host.snapshot('local');

        // WHEN: Final activity arrives.
        await directLine.emulateIncomingActivity(
          addLivestreamingMetadata(
            {
              from: {
                id: 'u-00001',
                name: 'Bot',
                role: 'bot'
              },
              id: 't-00003',
              text: 'A quick brown fox jumped over the lazy dogs.',
              type: 'message'
            },
            { streamId: 't-00001', streamType: 'final' }
          )
        );

        // THEN: Final activity should appear.
        await pageConditions.numActivitiesShown(1);
        await pageConditions.became(
          'final activity show up',
          () => pageElements.activityContents()[0].textContent === 'A quick brown fox jumped over the lazy dogs.',
          1_000
        );

        // THEN: Should show the informative message with blue bottom border.
        // THEN: Should not show typing indicator.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
